package base;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import exception.MyException;
import util.OnClickUtil;
import util.StatusBarUtils;
import util.TUtil;
import util.XPermissionUtils;


/**
 * Created by 刘智强 on 2017/4/11.
 * T-MVP模式的activit的基类, 可根据自己的业务逻辑更改方法,该模式下最好把所有的业务逻辑都写在一个presenter中
 */

public abstract class BaseActivity<T extends BasePresenter, M extends BaseModel> extends AppCompatActivity implements View.OnClickListener {

    public T presenter;
    public Activity mActivity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = this;
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//顶部状态栏透明
//            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);//底部虚拟键盘透明
        }

        if (getLayoutId() == 0) {
            try {
                throw new MyException("请先设置布局文件的ID");
            } catch (MyException e) {
                e.printStackTrace();
            }
        } else {
            setContentView(getLayoutId());
        }
        StatusBarUtils.setTranslucent(mActivity);//适用于图片作为背景的界面,此时需要图片填充到状态栏
/**
 * 让根布局延伸到状态栏，适用纯色的，如果是图片需要延伸到状态栏就不需要以下代码
 */
        ViewGroup contentFrameLayout = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);
        View parentView = contentFrameLayout.getChildAt(0);
        if (parentView != null && Build.VERSION.SDK_INT >= 14) {
            parentView.setFitsSystemWindows(true);
            //将主页面顶部延伸至status bar;虽默认为false,但经测试,DrawerLayout需显示设置
            parentView.setClickable(false);
        }


        presenter = TUtil.getClazz(this, 0);

        if (presenter == null) {
            try {
                throw new MyException("presenter对象为null");
            } catch (MyException e) {
                e.printStackTrace();
            }
        }
        presenter.setVM(TUtil.getT(this, 1), this);
        init();
        initView();
        initListener();

    }


    public abstract int getLayoutId();//获取布局文件的ID

    public abstract void initView();//初始化控件

    public abstract void initClick(View v);//点击事件的复写

    public abstract void initListener();//设置点击事件

    public abstract void init();//初始化

    public void addFragment(Fragment fragment, int fragment_Id) {
        if (fragment == null) {
            try {
                throw new MyException("请先初始化fragment");
            } catch (MyException e) {
                e.printStackTrace();
            }
        }
        if (fragment_Id == 0) {
            try {
                throw new MyException("请先设置fragment的布局文件ID");
            } catch (MyException e) {
                e.printStackTrace();
            }

        }
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(fragment_Id, fragment).commit();

    }//添加fragment

    /**
     * 给控件添加点击监听
     *
     * @param ids
     */
    public final void addOnClickListener(int... ids) {
        if (ids != null && ids.length > 0) {
            for (int id : ids) {
                View v = findViewById(id);
                if (v != null) {
                    v.setOnClickListener(this);
                }
            }

        }


    }

    @Override
    public void onClick(View v) {
        if (OnClickUtil.fastClick()) {
            initClick(v);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        XPermissionUtils.onRequestPermissionsResult(mActivity, requestCode, permissions, grantResults);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    }
}
